/**
 * Copyright 1993-2018 Agree Tech.
 * All rights reserved.
 */
package com.fjl.controller;

import com.fjl.pojo.ServiceResultInAbxMultiBranch;
import com.fjl.service.AbpmServiceInABXMultiBranch;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParserFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author PuYun pu.yun@agree.com.cn
 */
@RestController
public class AbpmController
{

    /**
     * 
     * @param jsonData
     * @return
     */
    private static Map<String, Object> jsonToMap(String jsonData)
    {
        if (jsonData == null || jsonData.length() == 0)
        {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            // 必须用jackson解析的json对象才可以被activiti维护，但jackson的语法比较严格
            // 自动格式转换，例如单引号转双引号
            jsonData = new JSONObject(jsonData).toString();
            map.putAll(JsonParserFactory.getJsonParser().parseMap(jsonData));
        } catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
        return map;
    }

//
//    @Autowired(required = false)
//    private Ignite ignite;
//
//    @Autowired
//    private RepositoryService repositoryService;
//
//    @Autowired
//    private RuntimeService runtimeService;

    @Autowired
    private AbpmServiceInABXMultiBranch abpmServiceInABXMultiBranch;

    @RequestMapping(path = "/startProcessWithAsync", method = RequestMethod.POST)
    public ServiceResultInAbxMultiBranch startProcessWithAsync(String definitionKey, String businessKey, String variablesJson) {
        return abpmServiceInABXMultiBranch.startProcess(definitionKey, businessKey, variablesJson);
    }
    @RequestMapping(path = "/continueProcessWithAsync", method = RequestMethod.PUT)
    public ServiceResultInAbxMultiBranch continueProcessWithAsync(@RequestParam(name = "taskId", required = false) String taskId,
                                                                  @RequestParam(name = "businessKey", required = false) String businessKey,
                                                                  @RequestParam(name = "variablesJson", required = false) String variablesJson){
        return abpmServiceInABXMultiBranch.continueProcess(taskId, businessKey, variablesJson);
    }



    /**
     * 查询当前任务节点的信息:taskId,taskKey,taskName
     *
     * @param rootProcessInstanceId
     * @return
     */
    @RequestMapping(path = "/queryCurrentTaskInfoWithAsync", method = RequestMethod.GET)
    public ServiceResultInAbxMultiBranch queryCurrentTaskInfoWithAsync(@RequestParam("rootProcessInstanceId") String rootProcessInstanceId)
    {
        ServiceResultInAbxMultiBranch currentTaskInfo = abpmServiceInABXMultiBranch.queryCurrentTaskInfo(rootProcessInstanceId);
        return currentTaskInfo;
    }

    /**
     * 查询流程
     * 
     * @param definitionKey
     *            流程定义ID
     * @return result
     */


    @RequestMapping(path = "/restartProcessWithAsync", method = RequestMethod.PUT)
    public ServiceResultInAbxMultiBranch restartProcessWithAsync(@RequestParam("definitionKey") String definitionKey,
                                                         @RequestParam("rootProcessInstanceId") String rootProcessInstanceId,
                                                         @RequestParam("variablesJson") String variablesJson)
    {
        return abpmServiceInABXMultiBranch.restartProcessWithAsync(definitionKey, rootProcessInstanceId, variablesJson);
    }

}
